package org.zhucy.forkJoin;

import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;

public class Task extends RecursiveAction{

    private static final long serialVersionUID = 1L;

    private List<Product> products;
    private int first;
    private int last;
    private double increment; //价格增长

    public Task(List<Product> products, int first, int last, double increment) {
        this.products = products;
        this.first = first;
        this.last = last;
        this.increment = increment;
    }

    @Override
    protected void compute() {
        if(last-first<5){
            updatePrices();
        }else{
            int middle=(last+first)/2;
            //System.out.printf("Task: Pending tasks: %s\n",getQueuedTaskCount());
            Task t1 = new Task(products,first,middle,increment);
            Task t2 = new Task(products,middle,last,increment);
            try {
                Thread.sleep(5000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            invokeAll(t1, t2);
        }
    }

    private void updatePrices() {
        for (int i=first; i<last; i++){
            System.out.println("i="+i);
            Product product=products.get(i);
            product.setPrice(product.getPrice()*(1+increment));
        }
    }

    public static void main(String[] args) {
        ProductListGenerator generator=new ProductListGenerator();
        List<Product> products=generator.generate(21);
        /*long s2 = System.currentTimeMillis();

        for(Product product:products){
            product.setPrice(product.getPrice()+1);
        }
        long e2 = System.currentTimeMillis();
        System.out.println("time2:"+(e2-s2));*/


        long s1 = System.currentTimeMillis();
        Task task=new Task(products, 0, products.size(), 0.20);
        ForkJoinPool pool = new ForkJoinPool();
        pool.invoke(task);
        //pool.execute(task);
        /*do {
            *//*System.out.printf("Main: Thread Count: %d\n",pool.getActiveThreadCount());
            System.out.printf("Main: Thread Steal: %d\n",pool.getStealCount());
            System.out.printf("Main: Parallelism: %d\n",pool.getParallelism());
            try {
                TimeUnit.NANOSECONDS.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }*//*
        } while (!task.isDone());

        if(task.isDone()){
            long e1 = System.currentTimeMillis();
            System.out.println("time1:"+(e1-s1));
        }
        pool.shutdown();
        if (task.isCompletedNormally()){
            System.out.printf("Main: The process has completed normally.\n");
        }*/
        /*for (int i=0; i<products.size(); i++){
            Product product=products.get(i);
            if (product.getPrice()!=12) {
                System.out.printf("Product %s: %f\n",product.getName(),product.getPrice());
            }else{
                System.out.println(product.getPrice());
            }
        }*/
        System.out.println("Main: End of the program.\n");

    }

}
